package cj.com.chabaike.ui.act.LonginAndJoin;

import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.balysv.materialmenu.MaterialMenuDrawable;
import com.balysv.materialmenu.MaterialMenuIcon;
import com.balysv.materialmenu.MaterialMenuIcon;

import java.util.ArrayList;
import java.util.List;

import cj.com.chabaike.R;

/**
 * Created by Administrator on 2015/8/10.
 */
public class LoginPager extends ActionBarActivity {
    private MaterialMenuDrawable menuIcon;
    private Toolbar mToolbar;
    private TabLayout mTab;
    private View view1,view2;
    private ViewPager mVp;
    private LoginPagerAdapter adapter;
    private List<View> mlist=new ArrayList<View >();
    private String[] list=new String[]{"登  录","注  册"};
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_layout);
        initToolbar();
        initTab();
        initViewPager();
    }



    private void initTab() {
        mTab= (TabLayout) findViewById(R.id.login_pager_Tab);
        mTab.setTabTextColors(Color.GRAY, Color.WHITE);

    }

    private void initToolbar() {
        mToolbar= (Toolbar) findViewById(R.id.toolbar_re);
        mToolbar.setTitle("");
        setSupportActionBar(mToolbar);

        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                onBackPressed();
               // finish();

            }
        });
//        mToolbar.setNavigationIcon(menuIcon);

        menuIcon=new MaterialMenuDrawable(this,Color.WHITE, MaterialMenuDrawable.Stroke.THIN);
        menuIcon.setIconState(MaterialMenuDrawable.IconState.ARROW);
        mToolbar.setNavigationIcon(menuIcon);
        menuIcon.setNeverDrawTouch(true);
    }
    private void initViewPager() {
        mVp = (ViewPager) findViewById(R.id.login_pager);
        view1= LayoutInflater.from(this).inflate(R.layout.login_layout_main,null);
        view2= LayoutInflater.from(this).inflate(R.layout.join_in_layout,null);
        mlist.add(view1);
        mlist.add(view2);

        adapter=new LoginPagerAdapter(mlist);
        mVp.setAdapter(adapter);
        mTab.setupWithViewPager(mVp);
        mTab.setTabsFromPagerAdapter(adapter);



    }
    class LoginPagerAdapter extends PagerAdapter{
        private List<View> mlist;

        public LoginPagerAdapter(List<View> mlist){
            this.mlist=mlist;
        }
        @Override
        public int getCount() {
            return mlist.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(mlist.get(position));
            return mlist.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(mlist.get(position));
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return list[position];
        }
    }
}
